package com.zsc.newbuds.controller;

import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.service.impl.UserServiceImpl;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @author D
 */
@Controller
public class RegisterController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisUtil redisUtil;
    /**
     * 用户注册
     * @param phone 手机号
     * @param password 密码
     * @param code 验证码
     * @return
     */
    @ResponseBody
    @PostMapping("/userRegister")
    public int register(String phone, String password, String code, HttpSession session){

        int flag = 1;
        //获取redis中的验证码进行匹配
        String redisCode = (String)redisUtil.get(phone);
        System.out.println("redisCode:"+redisCode);
        //相同则用户注册成功
        if(code.equals(redisCode)){
            //获取随机用户名
            String account = getRandomUserName(5);
            User user = new User();
            user.setUserName(account);
            user.setPassword(password);
            user.setIntegral(0);
            user.setPhone(phone);
            user.setAvatar("http://dd.ddandang.top/headportrait/test.jpeg");
            userService.addUser(user);
            session.setAttribute("user",user);
            session.setAttribute("userImg",user.getAvatar());
        }
        else {
            System.out.println("验证码不正确");
            flag = 0;
        }
        return flag;

    }
    @RequestMapping("/userRegister")
    public String toRegister(){
        return "redirect:UserRegister.html";
    }

    @RequestMapping("UserRegister.html")
    public String register(){
        return "UserRegister";
    }


    /**
     * 根据手机号判断用户是否存在
     * @param phone
     * @return
     */
    @ResponseBody
    @GetMapping("/judgeByPhone")
    public int judgeUser(String phone){
        return userService.isExist(phone);
    }


    /**
     * 随机生成用户名
     * @param len 长度
     * @return 用户名
     */
    public static String getRandomUserName(int len) {

        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String str = null;
            int highPas, lowPos;
            Random random = new Random();
            highPas = (176 + Math.abs(random.nextInt(39)));
            lowPos = (161 + Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (Integer.valueOf(highPas).byteValue());
            b[1] = (Integer.valueOf(lowPos).byteValue());
            try {
                str = new String(b, "GBK");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret.append(str);
        }
        return ret.toString();
    }
}
